    package com.shaoshaohuo.app.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

import com.shaoshaohuo.app.R;

    /**
 * Created by Administrator on 2017/6/27.
 */

public class MyImageView extends ImageView {


        private float dimension;

        public MyImageView(Context context) {
        this(context,  null);
    }

    public MyImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        int[] a=new int[]{R.attr.roundsize};
        TypedArray typedArray = getResources().obtainAttributes(attrs, R.styleable.MyImageView);
        dimension = typedArray.getDimension(R.styleable.MyImageView_roundsize, 10);
        typedArray.recycle();
    }
    @Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
        BitmapDrawable drawable = (BitmapDrawable) getDrawable();
        if (drawable == null) {
            return;
        }
        Bitmap bitmap = drawable.getBitmap();
        if (bitmap == null) {
            return;
        }
        int width = getWidth();
        int height = getHeight();
        int bitmapwidth = bitmap.getWidth();
        int bitmapheight = bitmap.getHeight();
        if (width > height) {
            //此时拉伸图片应该按照高来
//            while (width > height) {
//                Rect rect = new Rect();
//                rect.set(0, 0, bitmapwidth, bitmapheight);
//                height += 5;
//
//            }
            float i = width / height;
            Log.e("TAG", "onDraw: " + i);
//            canvas.drawBitmap(bitmap, new Rect(0, 0, bitmap.getWidth(), bitmapheight), new Rect(0, 0, (int) (getHeight() * i), getHeight()), null);
            Bitmap bitmap1 = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas1 = new Canvas(bitmap1);

            Paint paint = new Paint();
            paint.setAntiAlias(true);
//            canvas1.drawRoundRect(0, 0, getWidth(), getHeight(), 10, 10, paint);
            canvas1.drawRoundRect(new RectF(0, 0, getWidth(), getHeight()), dimension, dimension, paint);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            canvas1.drawBitmap(bitmap, new Rect(0, bitmapheight / 2 - getHeight() / 2, bitmap.getWidth(), bitmapheight / 2 + getHeight() / 2), new Rect(0, 0, getWidth(), getHeight()), paint);
            canvas.drawBitmap(bitmap1, 0, 0, null);
        } else if (width < height) {

        }
    }

    @Override
    public void setImageBitmap(Bitmap bm) {
        super.setImageBitmap(bm);
        invalidate();
    }

    @Override
    public void setImageDrawable(@Nullable Drawable drawable) {
        super.setImageDrawable(drawable);
        invalidate();
    }
}
